// app/src/main/java/com/example/snake/RankingActivity.java
package com.example.snake;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RankingActivity extends Activity {
    private ListView listView;
    private ScoreManager scoreManager;
    private Button btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);

        listView = findViewById(R.id.listView);
        scoreManager = new ScoreManager(this);
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(v -> finish());
        displayRankings();
    }

    private void displayRankings() {
        List<Integer> scores = scoreManager.getScores();
        List<Map<String, String>> rankingData = new ArrayList<>();

        for (int i = 0; i < scores.size(); i++) {
            Map<String, String> item = new HashMap<>();
            item.put("rank", String.valueOf(i + 1));
            item.put("score", String.valueOf(scores.get(i)));
            rankingData.add(item);
        }

        SimpleAdapter adapter = new SimpleAdapter(
                this,
                rankingData,
                R.layout.ranking_item,
                new String[]{"rank", "score"},
                new int[]{R.id.tvRank, R.id.tvScoreItem}
        );

        listView.setAdapter(adapter);

        // 如果没有分数记录，显示提示信息
        if (scores.isEmpty()) {
            TextView emptyText = findViewById(R.id.tvEmpty);
            emptyText.setText("暂无排名记录");
        }
    }
}
